<template>
  <div class="title">
    <div class="title-context">
      <img src="../assets/logo/logo.png">
      <h2 class="text">方盒子CRM</h2>
    </div>
    <div class="login">
      <el-tabs v-model="activeName" stretch>
        <el-tab-pane label="账户密码登录" name="first">
          <el-form ref="simpleForm" :model="simpleForm" :rules="simpleRules" class="login-form">
            <el-form-item prop="username">
              <el-input
                v-model="simpleForm.username"
                type="text"
                auto-complete="off"
                placeholder="用户名"
              >
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="simpleForm.password"
                type="password"
                auto-complete="off"
                placeholder="密码"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
              </el-input>
            </el-form-item>
            <el-checkbox v-model="simpleForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
            <div style="float: right;" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
            <el-form-item style="width:100%;">
              <el-button
                :loading="loading"
                size="medium"
                type="primary"
                style="width:100%;"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="邮箱登录" name="second">
          <el-form ref="emailForm" :model="emailForm" :rules="emailRules" class="login-form">
            <el-form-item prop="email">
              <el-input
                v-model="emailForm.email"
                type="text"
                auto-complete="off"
                placeholder="邮箱"
              >
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
              </el-input>
            </el-form-item>

            <el-form-item prop="code" v-if="true">
              <el-input
                v-model="emailForm.code"
                auto-complete="off"
                placeholder="验证码"
                style="width: 63%"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
              </el-input>
              <div class="login-code">
                <el-button plain @click="sendEmail()">获取验证码</el-button>
              </div>
            </el-form-item>
            <el-checkbox v-model="simpleForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
            <div style="float: right;" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
            <el-form-item style="width:100%;">
              <el-button
                :loading="loading"
                size="medium"
                type="primary"
                style="width:100%;"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>

      <!--  底部  -->
      <div class="el-login-footer" style="color: #1e1e1e">
        <a href="https://beian.miit.gov.cn/" target="_blank">备案号：</a>
        <a href="https://beian.miit.gov.cn/" target="_blank">鲁ICP备2021032146号-1</a>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { decrypt, encrypt } from '@/utils/jsencrypt'

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      activeName: 'first',
      simpleForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      emailForm: {
        email: '',
        code: ''
      },
      simpleRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      emailRules: {
        email: [
          { required: true, trigger: 'blur', message: '请输入您的邮箱' },
          {
            type: 'email',
            message: '请正确输入邮箱',
            trigger: ['blur', 'change']
          }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: true,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff
        if (this.captchaOnOff) {
          this.codeUrl = res.img
          this.codeUrl = res.img
          this.simpleForm.uuid = res.uuid
        }
      })
    },
    // 发送验证码
    sendEmail() {
      this.$refs.emailForm.validateField('email', valid => {
        if (valid) {
          // 发送验证码

        }
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.simpleForm = {
        username: username === undefined ? this.simpleForm.username : username,
        password: password === undefined ? this.simpleForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      if (this.activeName == 'first') {
        this.$refs.simpleForm.validate(valid => {
          if (valid) {
            this.loading = true
            if (this.simpleForm.rememberMe) {
              Cookies.set('username', this.simpleForm.username, { expires: 30 })
              Cookies.set('password', encrypt(this.simpleForm.password), { expires: 30 })
              Cookies.set('rememberMe', this.simpleForm.rememberMe, { expires: 30 })
            } else {
              Cookies.remove('username')
              Cookies.remove('password')
              Cookies.remove('rememberMe')
            }
            this.$store.dispatch('Login', this.simpleForm).then(() => {
              this.$router.push({ path: this.redirect || '/' }).catch(() => {
              })
            }).catch(() => {
              this.loading = false
              if (this.captchaOnOff) {
                this.getCode()
              }
            })
          }
        })
      } else {
        this.$refs.emailForm.validate(valid => {
          if (valid) {
            this.loading = true
            if (this.simpleForm.rememberMe) {
              Cookies.set('email', this.emailForm.email, { expires: 30 })
              Cookies.set('rememberMe', this.simpleForm.rememberMe, { expires: 30 })
            } else {
              Cookies.remove('email')
              Cookies.remove('rememberMe')
            }
            this.$store.dispatch('Login', this.simpleForm).then(() => {
              this.$router.push({ path: this.redirect || '/' }).catch(() => {
              })
            }).catch(() => {
              this.loading = false
              if (this.captchaOnOff) {
                this.getCode()
              }
            })
          }
        })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.title {
  height: 100%;
  background: #f0f2f5 url(../assets/images/background.svg) no-repeat 50%;

  .title-context {
    width: 100%;
    padding-top: 85px;
    text-align: center;
    padding-bottom: 55px;

    img {
      height: 44px;
      display: inline;
      margin-right: 20px
    }

    .text {
      line-height: 44px;
      font-size: 33px;
      color: rgba(0, 0, 0, .85);
      font-family: Avenir, Helvetica Neue, Arial, Helvetica, sans-serif;
      font-weight: 600;
      display: inline;
      height: 44px
    }
  }
}

.login {
  display: flex;
  justify-content: space-around;
  margin: 0 auto;

  .login-img {
    height: 100%;
    overflow: hidden;
    padding: 30px 0px;

    img {
      height: 100%;
      border-radius: 20px 0 0 20px;
    }
  }

  .login-form {
    border-radius: 6px;
    width: 400px;
    padding: 25px 25px 5px 25px;

    .el-input {
      height: 38px;

      input {
        height: 38px;
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }

    .login-code {
      height: 38px;
      float: right;
    }

  }

}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 14px;
  letter-spacing: 1px;
  margin-bottom: 12px;
}

::v-deep .el-tabs__content {
  transform: translateX(-50px);

}

@keyframes chang1 {
  from {
    transform: translateX(-50);
    opacity: 0.1;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}
</style>
